package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lemuel on 2016/12/5.
 */
public class ApiBeibokuUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiBeibokuUtil.class);

    private ApiBeibokuUtil() {
    }

    public static final String BEIBOKU_DELETE = Configuration.getString("beiboku.delete");

    public static boolean deleteBeiboku(long channelId, List<Long> qipuIdsList) {
        boolean res = false;
        Map<String, String> map = new HashMap<String, String>();
        map.put("qiPuIds", makeQipuIds(qipuIdsList));
        map.put("channelids", channelId + "");
        JSONObject result = ApiHttpRequestUtil.getMethod(BEIBOKU_DELETE, map);
        if (result != null && "0".equals(result.getString("error_code"))) {
            logger.info("[ApiBeibokuUtil][deleteBeiboku][success]{}", JSON.toJSONString(map));
            res = true;
        }
        logger.info("[ApiBeibokuUtil][deleteBeiboku][fail]{}", JSON.toJSONString(map));
        return res;
    }

    private static String makeQipuIds(List<Long> qipuIds) {
        int size = qipuIds.size();

        if (size <= 0) {
            return "";
        }

        StringBuffer qipuidsStr = new StringBuffer("");

        for (int i = 0; i < size; ++i) {
            qipuidsStr.append(qipuIds.get(i).longValue());
            if (i != size - 1) {
                qipuidsStr.append(",");
            }
        }

        return qipuidsStr.toString();
    }

//    public static void main(String[] args) {
//        List<Long> qipuIds = new ArrayList<>();
//        qipuIds.add(23242342342l);
//
//        System.out.println("qipuidsStr => " + makeQipuIds(qipuIds));
//
//        qipuIds.add(232342342222l);
//        qipuIds.add(232342342222l);
//        qipuIds.add(23234l);
//        qipuIds.add(23234l);
//        System.out.println("qipuidsStr => " + makeQipuIds(qipuIds));
//
//    }
}
